/*
 * Copyright (C) 2001-2003 Jacek Sieka, j_s@telia.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stdafx.h"

#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "Sounds.h"
#include "WinUtil.h"

PropPage::TextItem Sounds::texts[] =
{
	{ IDC_PRIVATE_MESSAGE_BEEP, ResourceManager::SETTINGS_PM_BEEP },
	{ IDC_PRIVATE_MESSAGE_BEEP_OPEN, ResourceManager::SETTINGS_PM_BEEP_OPEN },
	{ IDC_CZDC_PM_SOUND, ResourceManager::SETCZDC_PRIVATE_SOUND },
	{ IDC_BROWSE, ResourceManager::BROWSE },
	{ IDC_PLAY, ResourceManager::PLAY },
	{ IDC_NONE, ResourceManager::NONE },
	{ IDC_DEFAULT, ResourceManager::DEFAULT },
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item Sounds::items[] =
{
	{ IDC_PRIVATE_MESSAGE_BEEP, SettingsManager::PRIVATE_MESSAGE_BEEP, PropPage::T_BOOL },
	{ IDC_PRIVATE_MESSAGE_BEEP_OPEN, SettingsManager::PRIVATE_MESSAGE_BEEP_OPEN, PropPage::T_BOOL },
	{ 0, 0, PropPage::T_END }
};

Sounds::snds Sounds::sounds[] =
{
	{ ResourceManager::SOUND_DOWNLOAD_BEGINS,   SettingsManager::SOUND_BEGINFILE, ""},
	{ ResourceManager::SOUND_DOWNLOAD_FINISHED, SettingsManager::SOUND_FINISHFILE, ""},
	{ ResourceManager::SOUND_SOURCE_ADDED,  SettingsManager::SOUND_SOURCEFILE, ""},
	{ ResourceManager::SOUND_UPLOAD_FINISHED,   SettingsManager::SOUND_UPLOADFILE, ""},
	{ ResourceManager::SOUND_FAKER_FOUND,   SettingsManager::SOUND_FAKERFILE, ""},
	{ ResourceManager::SETCZDC_PRIVATE_SOUND,   SettingsManager::SOUND_BEEPFILE, ""},
	{ ResourceManager::MYNICK_IN_CHAT,  SettingsManager::SOUND_CHATNAMEFILE, ""},
	{ ResourceManager::SOUND_TTH_INVALID,   SettingsManager::SOUND_TTH, ""},
	{ ResourceManager::SOUND_EXCEPTION, SettingsManager::SOUND_EXC, ""},
	{ ResourceManager::HUB_CONNECTED,   SettingsManager::SOUND_HUBCON, ""},
	{ ResourceManager::HUB_DISCONNECTED,    SettingsManager::SOUND_HUBDISCON, ""},
	{ ResourceManager::FAVUSER_ONLINE,  SettingsManager::SOUND_FAVUSER, ""},
	{ ResourceManager::FAVUSER_OFFLINE,      SettingsManager::SOUND_FAVUSER_OFFLINE, ""},
	{ ResourceManager::SOUND_TYPING_NOTIFY, SettingsManager::SOUND_TYPING_NOTIFY, ""}
};

LRESULT Sounds::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);
	PropPage::read((HWND)*this, items);

		SetDlgItemText(IDC_SOUND_ENABLE, CTSTRING(ENABLE_SOUNDS));
		CheckDlgButton(IDC_SOUND_ENABLE, SETTING(SOUNDS_DISABLED) ? BST_UNCHECKED : BST_CHECKED);
	
	ctrlSounds.Attach(GetDlgItem(IDC_SOUNDLIST));
	
	ctrlSounds.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER);
	ctrlSounds.InsertColumn(0, CTSTRING(SETTINGS_SOUNDS), LVCFMT_LEFT, 172, 0);
	ctrlSounds.InsertColumn(1, CTSTRING(FILENAME), LVCFMT_LEFT, 210, 1);
	
	// Do specialized reading here
	
	int j;
	for (int i = 0; i < _countof(sounds); i++)
	{
		j = ctrlSounds.insert(i, Text::toT(ResourceManager::getInstance()->getString(sounds[i].name)).c_str());
		sounds[i].value = SettingsManager::getInstance()->get((SettingsManager::StrSetting)sounds[i].setting, true);
		ctrlSounds.SetItemText(j, 1, Text::toT(sounds[i].value).c_str());
	}
	
	return TRUE;
}


void Sounds::write()
{
	PropPage::write((HWND)*this, items);
	
	LocalArray<TCHAR, 256> buf;
	
	for (int i = 0; i <_countof(sounds); i++)
	{
		ctrlSounds.GetItemText(i, 1, buf, 255);
		settings->set((SettingsManager::StrSetting)sounds[i].setting, Text::fromT(buf));
	}
	
	SET_SETTING(SOUNDS_DISABLED, IsDlgButtonChecked(IDC_SOUND_ENABLE) == 1 ? false : true);
	// Do specialized writing here
	// settings->set(XX, YY);
}

LRESULT Sounds::onBrowse(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	LocalArray <TCHAR, MAX_PATH> buf;
	LVITEM item = {0};
	item.mask = LVIF_TEXT;
	item.cchTextMax = 255;
	item.pszText = buf;
	if (ctrlSounds.GetSelectedItem(&item))
	{
		tstring x = _T("");
		if (WinUtil::browseFile(x, m_hWnd, false) == IDOK)
		{
			ctrlSounds.SetItemText(item.iItem, 1, x.c_str());
		}
	}
	return 0;
}

LRESULT Sounds::onClickedNone(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	LocalArray <TCHAR, MAX_PATH> buf;
	LVITEM item = {0};
	item.mask = LVIF_TEXT;
	item.cchTextMax = 255;
	item.pszText = buf;
	if (ctrlSounds.GetSelectedItem(&item))
	{
		tstring x = _T("");
		ctrlSounds.SetItemText(item.iItem, 1, x.c_str());
	}
	return 0;
}

LRESULT Sounds::onPlay(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	LocalArray <TCHAR, MAX_PATH> buf;
	LVITEM item = {0};
	item.mask = LVIF_TEXT;
	item.cchTextMax = 255;
	item.pszText = buf;
	if (ctrlSounds.GetSelectedItem(&item))
	{
		ctrlSounds.GetItemText(item.iItem, 1, buf, MAX_PATH);
		PlaySound(buf, NULL, SND_FILENAME | SND_ASYNC);
		
	}
	return 0;
}
LRESULT Sounds::onDefault(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	LocalArray <TCHAR, MAX_PATH> buf;
	LVITEM item = {0};
	item.mask = LVIF_TEXT;
	item.cchTextMax = 255;
	item.pszText = buf;
	if (ctrlSounds.GetSelectedItem(&item))
	{
		tstring x = Text::toT(Util::getSoundPath());
		switch (ctrlSounds.GetSelectionMark())
		{
			case 0:
				x += _T("DownloadBegins.wav");
				break;
			case 1:
				x += _T("DownloadFinished.wav");
				break;
			case 2:
				x += _T("AltSourceAdded.wav");
				break;
			case 3:
				x += _T("UploadFinished.wav");
				break;
			case 4:
				x += _T("FakerFound.wav");
				break;
			case 5:
				x += _T("PrivateMessage.wav");
				break;
			case 6:
				x += _T("MyNickInMainChat.wav");
				break;
			case 7:
				x += _T("FileCorrupted.wav");
				break;
			case 8:
				x += _T("UnhandledException.wav");
				break;
			case 9:
				x += _T("HubConnected.wav");
				break;
			case 10:
				x += _T("HubDisconnected.wav");
				break;
			case 11:
				x += _T("FavUser.wav");
				break;
			case 12:
				x += _T("FavUserDisconnected.wav");
				break;
			case 13:
				x += _T("AltSourceAdded.wav");
				break;
		}
		ctrlSounds.SetItemText(item.iItem, 1, x.c_str());
	}
	return 0;
}

/**
 * @file
 * $Id: Sounds.cpp,v 1.11 2005/02/22 16:49:49 bigmuscle Exp $
 */

